# Maintainer: psykose <alice@ayaya.dev>
pkgname=firefox-developer-edition
pkgver=103.0_beta7
_pkgver=${pkgver/_beta/b}
_releasedate=2022-07-11
pkgrel=0
pkgdesc="Firefox web browser"
url="https://www.firefox.com/"
# s390x and riscv64 blocked by rust and cargo
# armhf: https://bugzilla.mozilla.org/show_bug.cgi?id=1680495
# ppc64le: ld.lld: error: undefined hidden symbol: something about video
# x86/armv7: who is using this there?
arch="x86_64 aarch64"
license="GPL-3.0-only AND LGPL-2.1-only AND LGPL-3.0-only AND MPL-2.0"
depends="ffmpeg-libs"
makedepends="
	alsa-lib-dev
	automake
	bsd-compat-headers
	cargo
	cbindgen
	clang-dev
	dbus-glib-dev
	gettext
	gtk+3.0-dev
	hunspell-dev
	icu-dev
	libevent-dev
	libffi-dev
	libidl-dev
	libjpeg-turbo-dev
	libnotify-dev
	libogg-dev
	libtheora-dev
	libtool
	libvorbis-dev
	libvpx-dev
	libwebp-dev
	libxcomposite-dev
	libxt-dev
	lld
	llvm-dev
	m4
	mesa-dev
	nasm
	nodejs
	nspr-dev
	nss-dev
	nss-static
	pipewire-dev
	pulseaudio-dev
	python3
	sed
	wasi-sdk
	wireless-tools-dev
	yasm
	zip
	"
source="https://ftp.mozilla.org/pub/firefox/releases/$_pkgver/source/firefox-$_pkgver.source.tar.xz
	stab.h

	fix-fortify-system-wrappers.patch
	mallinfo.patch

	disable-moz-stackwalk.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch

	disable-neon-in-aom.patch
	sandbox-fork.patch
	sandbox-sched_setscheduler.patch
	sandbox-largefile.patch

	avoid-redefinition.patch
	python-deps.patch

	install-dir.patch

	firefox-developer-edition.desktop
	distribution.ini
	vendor-prefs.js
	"
options="!check" # no tests
builddir="$srcdir/firefox-${pkgver/_*/}"

_mozappdir=/usr/lib/firefox-developer-edition

# help our shared-object scanner to find the libs
ldpath="$_mozappdir"
sonameprefix="$pkgname:"

# we need this because cargo verifies checksums of all files in vendor
# crates when it builds and gives us no way to override or update the
# file sanely... so just clear out the file list
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
}

prepare() {
	default_prepare
	cp "$srcdir"/stab.h toolkit/crashreporter/google-breakpad/src/
}

build() {
	export SHELL=/bin/sh
	export MOZILLA_OFFICIAL=1
	export USE_SHORT_LIBNAME=1
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
	export MOZ_APP_REMOTINGNAME=firefoxdeveloperedition
	export MOZBUILD_STATE_PATH="$srcdir"/mozbuild
	# allow unsigned addons for dev-edition
	export MOZ_REQUIRE_SIGNING=
	# disable desktop notifications
	export MOZ_NOSPAM=1
	# Find our triplet JSON
	export RUST_TARGET="$CTARGET"
	# Build with Clang, takes less RAM
	export CC="clang"
	export CXX="clang++"

	# set rpath so linker finds the libs
	export LDFLAGS="$LDFLAGS -Wl,-rpath,$_mozappdir"

	case "$CARCH" in
	aarch64|arm*|x86*)
		# disable-elf-hack: exists only on aarch64, arm*, x86, x86_64
		_arch_config="--disable-elf-hack"
		;;
	esac

	# webrtc does not build on these
	case "$CARCH" in
	armv7|ppc64le)
		_webrtc_config="--disable-webrtc"
		;;
	esac

	# FF doesn't have SIMD available on armhf/v7
	case "$CARCH" in
	arm*)
		_rust_simd="--disable-rust-simd"
		;;
	*)
		_rust_simd="--enable-rust-simd"
		;;
	esac

	# disable debug on 32-bit (takes too much link memory)
	case "$CARCH" in
	arm*|x86)
		_low_mem_flags="--disable-debug-symbols --disable-debug"
		# hardcoded in the file
		sed -i 's|debug_info = "2"|debug_info = "0"|' \
			../build/moz.configure/toolchain.configure
		;;
	esac

	# sandbox only supported here
	case "$CARCH" in
	x86*|armv7|aarch64)
		_sandbox="--enable-sandbox"
		;;
	*)
		_sandbox="--disable-sandbox"
		;;
	esac

	./mach configure \
		--prefix=/usr \
		$_arch_config \
		$_webrtc_config \
		$_low_mem_flags \
		$_rust_simd \
		$_sandbox \
		\
		--disable-cargo-incremental \
		--disable-crashreporter \
		--disable-install-strip \
		--disable-jemalloc \
		--disable-minify \
		--disable-profiling \
		--disable-strip \
		--disable-tests \
		--disable-updater \
		\
		--enable-alsa \
		--enable-dbus \
		--enable-default-toolkit=cairo-gtk3-wayland \
		--enable-ffmpeg \
		--enable-hardening \
		--enable-linker=lld \
		--enable-necko-wifi \
		--enable-official-branding \
		--enable-optimize="$CFLAGS -O2" \
		--enable-pulseaudio \
		--enable-release \
		--enable-system-ffi \
		--enable-system-pixman \
		--enable-update-channel=aurora \
		\
		--with-branding=browser/branding/aurora \
		--with-distribution-id=org.alpinelinux \
		--with-libclang-path=/usr/lib \
		--with-system-ffi \
		--with-system-icu \
		--with-system-jpeg \
		--with-system-libevent \
		--with-system-libvpx \
		--with-system-nspr \
		--with-system-nss \
		--with-system-pixman \
		--with-system-png \
		--with-system-webp \
		--with-system-zlib \
		--with-unsigned-addon-scopes=app,system \
		--allow-addon-sideload \
		--with-wasi-sysroot=/usr/share/wasi-sysroot
	./mach build
}

package() {
	DESTDIR="$pkgdir" ./mach install

	local _png
	for _png in ./browser/branding/aurora/default*.png; do
		local i=${_png%.png}
		i=${i##*/default}
		install -Dm644 "$_png" \
			"$pkgdir"/usr/share/icons/hicolor/"$i"x"$i"/apps/$pkgname.png
	done

	install -Dm644 browser/branding/aurora/content/about-logo.png \
		"$pkgdir"/usr/share/icons/hicolor/192x192/apps/$pkgname.png
	install -Dm644 browser/branding/aurora/content/about-logo@2x.png \
		"$pkgdir"/usr/share/icons/hicolor/384x384/apps/$pkgname.png
	install -Dm644 browser/branding/aurora/content/about-logo.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg

	install -Dm644 "$srcdir"/firefox-developer-edition.desktop \
		"$pkgdir"/usr/share/applications/org.mozilla.firefox-developer-edition.desktop

	install -Dm644 "$srcdir"/distribution.ini \
		"$pkgdir"/$_mozappdir/distribution/distribution.ini

	# install our vendor prefs
	install -Dm644 "$srcdir"/vendor-prefs.js \
		"$pkgdir"/$_mozappdir/browser/defaults/preferences/vendor.js

	# Replace duplicate binary with wrapper
	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	install -Dm755 /dev/stdin "$pkgdir"/usr/bin/firefox-developer-edition <<- EOF
	#!/bin/sh
	exec /usr/lib/$pkgname/firefox "\$@"
	EOF
	rm "$pkgdir"/$_mozappdir/firefox-bin
	ln -sfv /usr/bin/firefox-developer-edition "$pkgdir"/$_mozappdir/firefox-bin
}

sha512sums="
94e309447059e971e04ec24d288d3684b7c682eaa660351ba70db78e6994bbd53875041e5efa5d43031101964238696c107a95644827280bab7eb42b5ab881ba  firefox-103.0b7.source.tar.xz
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
2f4f15974d52de4bb273b62a332d13620945d284bbc6fe6bd0a1f58ff7388443bc1d3bf9c82cc31a8527aad92b0cd3a1bc41d0af5e1800e0dcbd7033e58ffd71  fix-fortify-system-wrappers.patch
a4a3e062661bda64d502d426c480ac9645345860118de9df9ffe6e0597738c70c11e5cdef2d4fd12c5e2ee30a09310159230524655a419a4f7e4eeeb0f3c06b0  mallinfo.patch
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
cd68b89e29e5f6379fbd5679db27b9a5ef70ea65e51c0d0a8137e1f1fd210e35a8cfb047798e9549bc7275606d7ec5c8d8af1335d29da4699db7acd8bc7ff556  fix-rust-target.patch
305c874fdea3096e9c4c6aa6520ac64bb1c347c4b59db8360096646593fe684c3b5377874d91cecd33d56d1410b4714fbdea2b514923723ecbeff79d51265d9b  fix-webrtc-glibcisms.patch
55eab1a02e19a19a1ee0e36b11097ab48a44200e07e543d91469967206854f39709c7c0bc31855559528e64642d610868140e9533f1c0e3bebc953353c142fa8  disable-neon-in-aom.patch
2518f2fc75b5db30058e0735f47d60fdf1e7adfaeee4b33fb2afb1bd9a616ce943fd88f4404d0802d4083703f4acf1d5ad42377218d025bc768807fbaf7e1609  sandbox-fork.patch
94433c5ffdbe579c456d95c5f053f61fcbab2f652fa90bc69dcc27d9a1507a8e5c677adeadae9a7a75cc9a55184c1040737f4dfd10b279c088ef016561e6f135  sandbox-sched_setscheduler.patch
b7d0a6126bdf6c0569f80aabf5b37ed2c7a35712eb8a0404a2d85381552f5555d4f97d213ea26cec6a45dc2785f22439376ed5f8e78b4fd664ef0223307b333e  sandbox-largefile.patch
b1cb2db3122634f66d2bae7066e76f2dcd455c464e021db4de3b0a08314df95cb667846081682db549dd2af8a00831cabe44a2420c66cdfb5e3b5fa7e6bd21d3  avoid-redefinition.patch
f3d419880cc7f043b6eb547894d486457d407640be2bd8b402eb3a534ccea39568f6d506fc44a3b29c94eb0dc6fc2bec6600d161786fd233d26b1dc8970f5ab4  python-deps.patch
ff471e5160e313a9657bcbb767a6a581bab9da053cced02bde38d1ef03349472ad3602dba026c10d676d55c1712af772dd2b7bdbfa72b5db498c9ce8a2133423  install-dir.patch
2ff0cb8e2eb94cee306b488adf6d7f4debbaff6155be3ed3eeee814cdb356e2e60fe38cc29d6c8d10079937fd2c930cfddf283977cf645395d31eaf76c7f0ac2  firefox-developer-edition.desktop
4fd66382983d406ce0cd3586239fb1110224aeb2f9eee6fc931d980a31d18739d001d1f152208a7f1ccaae80867c40bd3d2ac674484617532a8f38049dc745bd  distribution.ini
fc45bc3ffb9404e5338ea26a9f04807b40f6f516324972cddd48bedb91b8bd7c6b8d4e03a0209020f5e67b703bc4ff89389985791b9bd544a0fc3951e2dc338e  vendor-prefs.js
"
